import os
import json


def get_raw_json(area):
    os.mkdir(os.path.normcase(f"static-data/goal-{area}"))
    for i in range(1, 17 + 1):
        os.system(
            f"curl -X GET --header 'Accept: application/json' 'https://unstats.un.org/SDGAPI/v1/sdg/Goal/PivotData?goal={i}&areaCode={area}' > /mnt/E/学习资料/假期/2022-2023寒假/美赛/workspace/code/data-grabber/static-data/goal-{area}/{i}.json")


# with open("static-data/all-indicators.json") as f:
#     d = json.load(f)
# indicators = []
# for goal in d:
#     for target in goal["targets"]:
#         for indicator in target["indicators"]:
#             indicators.append(indicator["code"])


area = 1
result = []
for i in range(1, 17 + 1):
    with open(os.path.normcase(f"static-data/goal-{area}/{i}.json")) as f:
        d = json.load(f)["data"]
    for entry in d:
        all_info = [entry['indicator'],
                    '' if entry['age'] is None else entry['age'],
                    '' if entry['sex'] is None else entry['sex'],
                    '' if entry['series'] is None else entry['series'],
                    '' if entry['location'] is None else entry['location'],
                    ]
        years = [''] * 23
        for year in eval(entry["years"]):
            j = int(year["year"][1:-1]) - 2000
            if 22 >= j >= 0:
                years[j] = year['value']
        all_info.extend(years)
        result.append(f'{",".join(all_info)}\n')
with open('data/goal-1.csv', 'w') as f:
    f.writelines(result)
